//
//  MYXLoginViewController.swift
//  weibo
//
//  Created by MaYingxin on 16/4/17.
//  Copyright © 2016年 MaYingxin. All rights reserved.
//

import UIKit
import SVProgressHUD

class MYXLoginViewController: UIViewController {

    
    override func loadView() {
        
        view = webView
        
        webView.delegate = self
        
        let url: NSURL = NSURL(string: "https://api.weibo.com/oauth2/authorize?client_id=\(AppKey)&redirect_uri=\(Redirect_uri)")!
        
        let request = NSURLRequest(URL: url)
        
        webView.loadRequest(request)
        
    }
    
    private lazy var webView: UIWebView = UIWebView()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setUI()
    }
    
    @objc private func cancle(){
        
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    @objc private func Done(){
        
        print("点击自动填充按钮啦")
        webView.stringByEvaluatingJavaScriptFromString("document.getElementById('passwd').value='\(WeiBoPwd)';")
        webView.stringByEvaluatingJavaScriptFromString("document.getElementById('userId').value='\(WeiBoUserName)';")
        
        webView.stringByEvaluatingJavaScriptFromString("document.getElementsByTagName('label')[4].remove();")
    }
    
    
    
    

    private func setUI(){
        
        view.backgroundColor = UIColor.whiteColor()
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "cancle")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(MYXLoginViewController.Done))
        navigationItem.title = "登录"
    }
    
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewWillDisappear(animated: Bool) {
        
        SVProgressHUD.dismiss()
    }
}

extension MYXLoginViewController: UIWebViewDelegate{
    
    func webViewDidStartLoad(webView: UIWebView) {
        
        SVProgressHUD.show()
    }
    
    func webViewDidFinishLoad(webView: UIWebView) {
        
        SVProgressHUD.dismiss()
    }
    
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
//        print(request)
        let url = request.URL?.absoluteString
        print(url)
//        print(request.URL?.absoluteString)
        print(Redirect_uri)
        print(url!.hasPrefix(Redirect_uri))
        
        if let urlStr = request.URL?.absoluteString where urlStr.hasPrefix(Redirect_uri){
            
            if let query = request.URL?.query {
                
                let code = query.substringFromIndex("code=".endIndex)
                
                print(code)
            }
        }
        return true
    }
    
    
}







